package com.haopt.clock.utils;

import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @package: cn.yungtay.shelf.utils
 * @description: 对对象的“”和null进行判断的工具类。
 * @author: haopt
 * @date: 2020 0810
 **/
@Slf4j
public class DataUtil {

    public static boolean isNullOrEmpty(Object obj) {
        if (obj == null) {
            return true;
        }
        if (obj instanceof CharSequence) {
            return ((CharSequence) obj).length() == 0;
        }
        if (obj instanceof Collection) {
            return ((Collection) obj).isEmpty();
        }
        if (obj instanceof Map) {
            return ((Map) obj).isEmpty();
        }
        if (obj instanceof Object[]) {
            Object[] object = (Object[]) obj;
            if (object.length == 0) {
                return true;
            }
            boolean empty = true;
            for (Object o : object) {
                if (!isNullOrEmpty(o)) {
                    empty = false;
                    break;
                }
            }
            return empty;
        }
        return false;
    }

    public static String getString(Map<String, Object> params, String key) {
        if (DataUtil.isNullOrEmpty(params.get(key))) {
            return "";
        }
        String s = params.get(key).toString();
        try {
            Double d = Double.parseDouble(s);
            if (d == d.intValue()) {
                return d.intValue() + "";
            }
            return new BigDecimal(s).toPlainString();
        } catch (Exception e) {
            return s;
        }
    }

    public static Date parse(String dateString) {
        Date date = new Date();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年 MM月dd日 hh:mm");
            date = sdf.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
            log.info("输入时间格式有误，请重新输入");
        }
        return date;
    }

    public static Map<String, String> format(Date date) {
        Map<String, String> map = new HashMap<>();
        try {
            DateFormat df5 = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.CHINA); //显示日期,上下午，时间（精确到秒）
            String s = df5.format(date);
            String[] s1 = s.split(" ");
            map.put("year", s1[0]);
            map.put("period", s1[1].substring(0, 2));
            map.put("hf", s1[1].substring(2, 8));
        } catch (Exception e) {
            e.printStackTrace();
            log.info("输入时间格式有误，请重新输入");
        }
        return map;
    }

    public static String currentTime() {
        String time = "";
        try {
            // 格式化时间
            SimpleDateFormat sdf = new SimpleDateFormat();
            // a为am/pm的标记
            sdf.applyPattern("yyyy-MM-dd");
            // 获取当前时间
            Date date = new Date();
            time = sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("输入时间格式有误，请重新输入");
        }
        return time;
    }



    public static String getAppointmentMonth(Date date) {
        String appointmentMonth = "";
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            appointmentMonth = cal.get(Calendar.MONTH) + "月";
            appointmentMonth = appointmentMonth + cal.get(Calendar.DATE) + "日 ";
        } catch (Exception e) {
            e.printStackTrace();
            log.info("输入时间格式有误，请重新输入");
        }
        return appointmentMonth;
    }

    /**
     * 获取当前时间，字符串格式
     *
     * @return
     */
    public static String getCurrentTime() {
        Date a = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String f = sdf.format(a);
        return f;
    }



    /**
     * byte数组加上 星期01-07
     * @param makeNumber
     * @return
     */
    public static byte[] getDataSend(byte[] makeNumber) {
        //创建一个时间对象
        Calendar now = Calendar.getInstance();
        int week = now.get(Calendar.DAY_OF_WEEK);
        //返回新的byte数组
        byte[] newByte = new byte[7];
        for (int i = 0; i < makeNumber.length; i++) {
            newByte[i] = (byte) (makeNumber[i] + (byte) week);
        }
        return newByte;
    }

    /**
     * 获取前一天时间的开始和结尾
     */
    public static Long getBeforeDay(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.add(Calendar.DATE, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        return calendar.getTime().getTime();
    }


}
